#include <assert.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>

#include "mcRasterColor.h"
#include "opencv2\highgui\highgui.hpp"

// load an image file (.bmp,.tif,.gif,...) into raster color
//  return: number of bits per pixel in the original image
int LoadImageFile2Raster(Raster_Color<uchar>&image, const char *pname_file)
{
	/*
	// open and load the file using the default load option
	FIBITMAP*	pfi_bitmap;

	// load an input image
	pfi_bitmap = GenericLoader(pname_file, 0);
	if (pfi_bitmap == NULL)
		return 0;

	int		bpp,row,i,j,k,t;
	BYTE	value,temp,*pdata_line;

	// data format
	bpp = FreeImage_GetBPP(pfi_bitmap);
	// image size
	image.SetDimension(FreeImage_GetHeight(pfi_bitmap), FreeImage_GetWidth(pfi_bitmap));
	// read the data
	switch(bpp)
	{
	case 1:
			for (row=image.rows()-1; row>=0; row--)
			{
				// upsidedown
				j = image.rows()-1-row;
				// get scan line
				pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
				i = 0;
				t = 0;
				while (i <image.cols())
				{
					value = pdata_line[t];
					for (k=0; k<8; k++)
					{
						temp = value & 0x80;
						if (temp != 0)
							temp = 255;

						image.R(j,i) = temp;
						image.G(j,i) = temp;
						image.B(j,i) = temp;

						value = value << 1;
						i++;
						if (i >= image.cols())
							break;
					}
					t++;
				}
			}
			break;
	case 4:
			for (row=image.rows()-1; row>=0; row--)
			{
				// upsidedown
				j = image.rows()-1-row;
				// get scan line
				pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
				i = 0;
				t = 0;
				while (i <image.cols())
				{
					value = pdata_line[t];
					for (k=0; k<2; k++)
					{
						temp = value & 0xF0;
						temp = temp >> 4;

						image.R(j,i) = temp;
						image.G(j,i) = temp;
						image.B(j,i) = temp;

						value = value << 4;
						i++;
						if (i >= image.cols())
							break;
					}
					t++;
				}
			}
			break;
	case 8:
			for (row=image.rows()-1; row>=0; row--)
			{
				// upsidedown
				j = image.rows()-1-row;
				// get scan line
				pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
				for (i=0; i<image.cols(); i++)
				{
					image.R(j,i) = pdata_line[i];
					image.G(j,i) = pdata_line[i];
					image.B(j,i) = pdata_line[i];
				}
			}
			break;
	case 16:
	case 24:
			for (row=image.rows()-1; row>=0; row--)
			{
				// upsidedown
				j = image.rows()-1-row;
				// get scan line
				pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
				for (i=0; i<image.cols(); i++)
				{
					image.B(j,i) = pdata_line[i*3];
					image.G(j,i) = pdata_line[i*3+1];
					image.R(j,i) = pdata_line[i*3+2];
				}
			}
			break;
	case 32:
			for (row=image.rows()-1; row>=0; row--)
			{
				// upsidedown
				j = image.rows()-1-row;
				// get scan line
				pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
				for (i=0; i<image.cols(); i++)
				{
					image.B(j,i) = pdata_line[i*4];
					image.G(j,i) = pdata_line[i*4+1];
					image.R(j,i) = pdata_line[i*4+2];
				}
			}
			break;
	}


	// free allocated space
	FreeImage_Unload(pfi_bitmap);

	return bpp;
	*/
	return false;
}


int LoadImageFile2Raster(Raster<uchar>&image, const char *pname_file)
{
	/*
	Raster_Color<uchar> image_color;

	int	num_bits;

	// load the image into an image in color
	num_bits = LoadImageFile2Raster(image_color, pname_file);
	if (num_bits <= 0)
		return num_bits;	// failed
	image_color.GrayImage(image);
	return num_bits;
	*/
	return false;
}

// 24 bits image
bool SaveRaster2ImageFile(Raster_Color<uchar>&image, const char *pname_file, const int dpi)
{
	/*
	FIBITMAP*	pfi_bitmap;

	// allocate space
	pfi_bitmap = FreeImage_Allocate(image.Width(), image.Height(), 24);
	if (pfi_bitmap == NULL)
		return false;
	// copy pixel value
	int		row,i,j;
	BYTE	*pdata_line;

	for (row=image.rows()-1; row>=0; row--)
	{
		// upsidedown
		j = image.rows()-1-row;
		// get scan line
		pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
		for (i=0; i<image.cols(); i++)
		{
			pdata_line[i*3] = image.B(j,i);
			pdata_line[i*3+1] = image.G(j,i);
			pdata_line[i*3+2] = image.R(j,i);
		}
	}

	BITMAPINFOHEADER *pbitmap_header;

	pbitmap_header = FreeImage_GetInfoHeader(pfi_bitmap);
	pbitmap_header->biXPelsPerMeter = 11811*dpi/300;
	pbitmap_header->biYPelsPerMeter = 11811*dpi/300;

	bool bsuccess=GenericWriter(pfi_bitmap, pname_file, 0);

	// free allocated space
	FreeImage_Unload(pfi_bitmap);

	return bsuccess;
	*/
	return false;
}


bool SaveRaster2ImageFile(Raster<uchar>&image, const char *pname_file, const int dpi)
{
	//cv::imwrite(pname_file, image.data);

	/*
	FIBITMAP*	pfi_bitmap;

	// allocate space
	pfi_bitmap = FreeImage_Allocate(image.cols(), image.rows(), 8);
	if (pfi_bitmap == NULL)
		return false;
	// copy pixel value
	int		row,i,j;
	BYTE	*pdata_line;

	for (row=image.rows()-1; row>=0; row--)
	{
		// upsidedown
		j = image.rows()-1-row;
		// get scan line
		pdata_line = FreeImage_GetScanLine(pfi_bitmap, row);
		for (i=0; i<image.cols(); i++)
			pdata_line[i] = image(j, i);
	}

	// set dpi
	BITMAPINFOHEADER *pbitmap_header;

	pbitmap_header = FreeImage_GetInfoHeader(pfi_bitmap);
	pbitmap_header->biXPelsPerMeter = 11811*dpi/300;
	pbitmap_header->biYPelsPerMeter = 11811*dpi/300;

	// build a greyscale pallette
	RGBQUAD *pal = FreeImage_GetPalette(pfi_bitmap);
	for (i=0; i<256; i++)
	{
		pal[i].rgbBlue = i;
		pal[i].rgbGreen = i;
		pal[i].rgbRed = i;
	}

	bool bsuccess= GenericWriter(pfi_bitmap, pname_file, 0);

	// free allocated space
	FreeImage_Unload(pfi_bitmap);

	return bsuccess;
	*/
	return false;
}


